package org.example.MQ.rabbitmq.PublishAndSubscribe;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.example.MQ.rabbitmq.config.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProductorSubAndPub1 {
    /**
     * 交换机名称
     */
    private static final String EXCHANGE_NAME = "SubAndPubExchange1";
    private static final String ROUTING_KEY = "";
 
    public static void main(String[] args) throws Exception {
        Connection connection =  ConnectionUtil.getConnection();
        Channel channel = null;
        try {
            channel = connection.createChannel();
            String msg = "交换机1发出的消息：hello world";
            //生产者生产的消息可以被投放在所有的绑定的队列中。交换器为fanout类型
            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, msg.getBytes("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                channel.close();
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
        }
 
    }
}